﻿using aier.business.logic;
using aier.dataaccess.model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace aier.wx.api.selfhost.Controllers
{
    //[Authorize]
    [RoutePrefix("api/Lead")]
    public class LeadController : ApiController
    {
        public LeadBll leadBll { get; set; }

        /// <summary>
        /// 添加潜在客户
        /// </summary>
        /// <param name="leadInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [ActionName("SaveLeadInfo")]
        public IHttpActionResult SaveLeadInfo(LeadInfo leadInfo) {          
            return Ok(leadBll.SaveLeadInfo(leadInfo));
        }


        /// <summary>
        /// 根据姓名、手机、身份证分页查询潜在客户
        /// </summary>
        /// <param name="condition">查询条件为：姓名、手机、身份证</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        [ActionName("QueryLeadPageByConditions")]
        public IHttpActionResult QueryLeadPageByConditions(string condition,int pageNum,int pageSize)
        {
            PageResult<LeadView> leadList = leadBll.QueryLeadInfoList(condition, pageNum, pageSize);
            return Ok<PageResult<LeadView>>(leadList);
        }

        /// <summary>
        /// 根据姓名、手机、身份证和负责人查询潜在客户
        /// Louis fixed 2019-10-9
        /// </summary>
        /// <param name="condition">查询条件为：姓名、手机、身份证</param>
        /// <param name="pageNum">页码</param>
        /// <param name="pageSize">每页大小</param>
        /// <param name="ownerid">负责人</param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        [ActionName("QueryLeadPageByConditionsAndOwner")]
        public IHttpActionResult QueryLeadPageByConditions(string condition, int pageNum, int pageSize,Guid ownerid)
        {
            PageResult<LeadView> leadList = leadBll.QueryLeadInfoList(condition, pageNum, pageSize,ownerid);
            return Ok<PageResult<LeadView>>(leadList);
        }

        /// <summary>
        /// 筛查统计 链接到search4Stat 页面执行的查询潜在客户
        /// Louis Created on 2019-11-4
        /// </summary>
        /// <param name="searchKey"></param>
        /// <param name="pageNum"></param>
        /// <param name="pageSize"></param>
        /// <param name="ownerid"></param>
        /// <param name="screenteamid"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        [ActionName("QueryLeadPageByScreenStat")] 
        public IHttpActionResult QueryLeadPageByScreenStat(string searchKey, int pageNum, int pageSize, Guid ownerid, Guid screenteamid, DateTime startDate, DateTime endDate, int type) {
            if (searchKey == null) searchKey = string.Empty;
            startDate = startDate.AddHours(-8);
            if (endDate.Date == DateTime.Now.Date) //前端统计是t+1,然后—8小时时区
            {
                endDate = endDate.AddHours(16);
            }
            else
            {
                endDate = endDate.AddHours(-8);
            }
            PageResult<LeadView> leadList = leadBll.QueryLeadInfoList4Stat(searchKey, pageNum, pageSize, ownerid,screenteamid,startDate,endDate,type);
            return Ok<PageResult<LeadView>>(leadList);
        }

        /// <summary>
        /// 根据姓名、手机、身份证查询潜在客户
        /// 查询条件为：姓名、手机、身份证
        /// 原程序 LeadBaseServiceImpl/select
        /// </summary>
        /// <param name="customerName">姓名</param>
        /// <param name="phone">手机</param>
        /// <param name="sfz">身份证</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryLeadInfoByConditions")]
        public IHttpActionResult QueryLeadInfoByConditions(string customerName, string phone,string sfz="")
        {
            LeadView lead = leadBll.QueryLeadInfoByConditions(customerName, phone, sfz);
            return Ok<LeadView>(lead);
        }

        /// <summary>
        /// 根据客户ID查询客户信息
        /// 查询条件为：客户ID
        /// 原程序 LeadBaseServiceImpl/select
        /// </summary>
        /// <param name="leadId"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryLeadInfoById")]
        public IHttpActionResult QueryLeadInfoById(Guid leadId)
        {
            LeadView lead = leadBll.QueryLeadInfoById(leadId);
            return Ok<LeadView>(lead);
        }
        /// <summary>
        /// 根据条件查询leadid
        /// 查询条件为：姓名、负责人ID、身份证、手机号码、座机
        /// 原程序 LeadBaseServiceImpl/queryLeadid
        /// </summary>
        /// <param name="customername">姓名</param>
        /// <param name="ownerid">负责人ID</param>
        /// <param name="citizenid">身份证</param>
        /// <param name="iphoneno">手机号码</param>
        /// <param name="telno">座机</param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("QueryLeadidByConditions")]
        public IHttpActionResult QueryLeadidByConditions(Guid ownerid, string customername="",  string citizenid = "", string iphoneno = "", string telno = "")
        {
            Guid lead = leadBll.QueryLeadidByConditions(customername, ownerid, citizenid, iphoneno, telno);
            return Ok<Guid>(lead);
        }


        /// <summary>
        /// 根据任务Id获取筛查信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetAcountDetailsData")]
        public IHttpActionResult GetAcountDetailsData(Guid leadId)
        {
            //UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            //var ownerId = userInfo.userid;
            var accountDetailView = leadBll.GetAccountDetailData(leadId);
            return Ok(accountDetailView);
        }

        /// <summary>
        /// 根据任务Id获取筛查信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetAccountScreenDetailData")]
        public IHttpActionResult GetAccountScreenDetailData(Guid leadId,Guid screenId)
        {
            //UserInfo userInfo = apiTool.GetLoginUser(RequestContext);
            //var ownerId = userInfo.userid;
            var accountDetailView = leadBll.GetAccountScreenDetailData(leadId, screenId);
            return Ok(accountDetailView);
        }

        /// <summary>
        /// 分组显示班级内学生
        /// </summary>
        /// <param name="classId"></param>
        /// <param name="schoolId"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("QueryLeadInfoBySchoolIdAndClassNum")]
        public IHttpActionResult QueryLeadInfoBySchoolIdAndClassNum(string classId, string schoolId = "")
        {
            var leadList = leadBll.QueryLeadInfoBySchoolIdAndClassNum(schoolId, classId);
            return Ok(leadList);
        }



        /// <summary>
        /// 查询学生信息根据潜在顾客id
        /// </summary>
        /// <param name="leadId"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        [ActionName("QueryStudentInfoById")]
        public IHttpActionResult QueryStudentInfoById(string leadId)
        {
            var leadList = leadBll.QueryStudentInfoById(leadId);
            return Ok(leadList);
        }

    }
}
